package com.lanling.educenter.controller;


import com.lanling.educenter.entity.Admin;
import com.lanling.educenter.entity.vo.AdminVo;
import com.lanling.educenter.other.R;
import com.lanling.educenter.service.AdminService;
import com.lanling.educenter.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡振华
 * @since 2021-02-13
 */
@RestController
@RequestMapping("/educenter/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    //管理员登录
    @PostMapping("login")
    public R login(@RequestBody Admin admin){
        Admin admin1= adminService.login(admin);
        //登录成功
        //生成token字符串，使用jwt工具类
        String token = JwtUtils.getJwtToken(admin1.getId(), admin1.getNickname());
        return R.ok().data("token",token).data("admin",admin1);
    }

    //获取用户真实姓名、角色和头像
    @GetMapping("info")
    public R info(HttpServletRequest request){
        Admin admin = adminService.info(request);
        return R.ok().data("admin",admin).data("roles","admin");
    }

    //获取用户全部信息
    @GetMapping("getProfile")
    public R getProfile(HttpServletRequest request){
        Admin admin = adminService.info(request);
        return R.ok().data("admin",admin);
    }

    //修改用户信息
    @PostMapping("updateProfile")
    public R updateProfile(@RequestBody Admin admin){
        Admin updateAdmin = adminService.updateProfile(admin);
        return R.ok().data("admin",updateAdmin);
    }

    //修改管理员真实姓名
    @PostMapping("updateNickname")
    public R updateNickname(@RequestBody Admin admin){
        String token = adminService.updateNickname(admin);
        return R.ok().data("token",token);
    }

    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody AdminVo adminVo){
        Admin admin = adminService.updatePassword(adminVo);
        return R.ok().data("admin",admin);
    }

}